﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using JustMVCTest.Models;
using Telerik.Web.Mvc;
using System.Text;
using System.IO;
using System.Collections;
using Telerik.Web.Mvc.Extensions;
using System.Diagnostics;

namespace JustMVCTest.Controllers
{
    public class AllPOsListController : Controller
    {
        private PODBContext db3 = new PODBContext();
        //private Papi_UsersDBContext db4 = new Papi_UsersDBContext();
        //private CostCenterDBContext db5 = new CostCenterDBContext();
        //
        // GET: /AllPOsList/

        public ActionResult AllPOsExportTableToCsv()
        {
            var user_id = User.Identity.Name;
            user_id = user_id.Replace("\\", ",");
            var words = user_id.Split(new Char[] { ',' });
            user_id = words[1];

            var pos_list = from poes in db3.POs
                           where (poes.latest == 1)
                           select poes;

            var mail_add = from users in db3.Papi_Users_tbl
                           where (users.domain_username == user_id)
                           select users.email;
            var users_list = from users in db3.Papi_Users_tbl
                             where (users.domain_username == user_id)
                             select users;

            var is_superuser = false;

            if (users_list.Count() > 0)
            {
                is_superuser = users_list.First().is_superuser;
            }

            var cost_cent = from users in db3.Papi_Users_tbl
                            where (users.domain_username == user_id)
                            select users.Cost_Cent;
            string[] cost_centArray = cost_cent.ToArray();
            String scost_cent = cost_centArray[0];

            var int_Order = from users in db3.Papi_Users_tbl
                            where (users.domain_username == user_id)
                            select users.Int_Order;

            string[] int_OrderArray = int_Order.ToArray();
            String sint_Order = int_OrderArray[0];

            var requisitioner = from users in db3.Papi_Users_tbl
                                where (users.domain_username == user_id)
                                select users.Requisitio;

            string[] requisitionerArray = requisitioner.ToArray();
            String sRequisitioner = requisitionerArray[0];

            IQueryable<PO> pos_req = null;
            IQueryable<PO> pos_cc = null;
            IQueryable<PO> pos_io = null;
            IQueryable<PO> pos = null;

            if (!is_superuser)
            {
                pos_req = (from users in db3.Papi_Users_tbl
                           where (users.domain_username == user_id)
                           join poes in pos_list
                           on users.Requisitio equals poes.Requisitioner
                           select poes);

                pos_cc = (from users in db3.Papi_Users_tbl
                          where (users.domain_username == user_id)
                          join poes in pos_list
                          on users.Cost_Cent equals poes.Cost_Cent
                          select poes);
                pos_io = (from users in db3.Papi_Users_tbl
                          where (users.domain_username == user_id)
                          join poes in pos_list
                          on users.Int_Order equals poes.Int_Order
                          select poes);
                pos = (from poes in pos_req select poes)
                      .Union
                      (from poes1 in pos_cc select poes1)
                      .Union
                      (from poes2 in pos_io select poes2);
            }

            else
            {
                pos = pos_list;
            }

            string FilterString1 = db3.Papi_Users_tbl.Where(p => p.domain_username == user_id).FirstOrDefault().filter;
            if (string.IsNullOrEmpty(FilterString1))
            {
                FilterString1 = "PONum=&ItemNum=&DeliveryDateFrom=&DeliveryDateTo=&Status=3&Vendor=&Fields=";
            }
            MemoryStream output = new MemoryStream();
            StreamWriter writer = new StreamWriter(output, Encoding.UTF8);

            string[] splitArray = FilterString1.Split('&');

            string[] PONumValue = splitArray[0].Split('=');
            ViewData["PONumValue"] = PONumValue[1];

            string[] ItemNumValue = splitArray[1].Split('=');
            ViewData["ItemNumValue"] = ItemNumValue[1];

            string[] DeliveryDateFromValue = splitArray[2].Split('=');
            ViewData["DeliveryDateFromValue"] = DeliveryDateFromValue[1];

            string[] DeliveryDateToValue = splitArray[3].Split('=');
            ViewData["DeliveryDateToValue"] = DeliveryDateToValue[1];

            string[] StatusValue = splitArray[4].Split('=');
            ViewData["StatusValue"] = StatusValue[1];

            string[] VendorValue = splitArray[5].Split('=');
            ViewData["VendorValue"] = VendorValue[1];

            string[] FieldsValue = splitArray[6].Split('=');

            if (FieldsValue[1].Contains("Description_Field") == true)
            {
                ViewData["Description_Field"] = true;
            }
            else
            {
                ViewData["Description_Field"] = false;
            }

            if (FieldsValue[1].Contains("VendorName_Field") == true)
            {
                ViewData["VendorName_Field"] = true;
            }
            else
            {
                ViewData["VendorName_Field"] = false;
            }

            if (FieldsValue[1].Contains("Delivery_Date_Field") == true)
            {
                ViewData["Delivery_Date_Field"] = true;
            }
            else
            {
                ViewData["Delivery_Date_Field"] = false;
            }

            if (FieldsValue[1].Contains("Status_Field") == true)
            {
                ViewData["Status_Field"] = true;
            }
            else
            {
                ViewData["Status_Field"] = false;
            }

            if (FieldsValue[1].Contains("Requisitioner_Field") == true)
            {
                ViewData["Requisitioner_Field"] = true;
            }
            else
            {
                ViewData["Requisitioner_Field"] = false;
            }

            if (FieldsValue[1].Contains("DeliveryDate_Req_Field") == true)
            {
                ViewData["DeliveryDate_Req_Field"] = true;
            }
            else
            {
                ViewData["DeliveryDate_Req_Field"] = false;
            }

            if (FieldsValue[1].Contains("Cost_Cent_Field") == true)
            {
                ViewData["Cost_Cent_Field"] = true;
            }
            else
            {
                ViewData["Cost_Cent_Field"] = false;
            }

            if (FieldsValue[1].Contains("Int_Order_Field") == true)
            {
                ViewData["Int_Order_Field"] = true;
            }
            else
            {
                ViewData["Int_Order_Field"] = false;
            }

            if (FieldsValue[1].Contains("Prof_Cent_Field") == true)
            {
                ViewData["Prof_Cent_Field"] = true;
            }
            else
            {
                ViewData["Prof_Cent_Field"] = false;
            }

            if (FieldsValue[1].Contains("Percentage_Field") == true)
            {
                ViewData["Percentage_Field"] = true;
            }
            else
            {
                ViewData["Percentage_Field"] = false;
            }

            if (FieldsValue[1].Contains("cost_elem_Field") == true)
            {
                ViewData["cost_elem_Field"] = true;
            }
            else
            {
                ViewData["cost_elem_Field"] = false;
            }


            if (FieldsValue[1].Contains("co_el_name_Field") == true)
            {
                ViewData["co_el_name_Field"] = true;
            }
            else
            {
                ViewData["co_el_name_Field"] = false;
            }

            if (FieldsValue[1].Contains("Vendor_Field") == true)
            {
                ViewData["Vendor_Field"] = true;
            }
            else
            {
                ViewData["Vendor_Field"] = false;
            }

            if (FieldsValue[1].Contains("UM_Field") == true)
            {
                ViewData["UM_Field"] = true;
            }
            else
            {
                ViewData["UM_Field"] = false;
            }

            if (FieldsValue[1].Contains("T_Curr_Field") == true)
            {
                ViewData["T_Curr_Field"] = true;
            }
            else
            {
                ViewData["T_Curr_Field"] = false;
            }

            if (FieldsValue[1].Contains("Sch_Lin_Field") == true)
            {
                ViewData["Sch_Lin_Field"] = true;
            }
            else
            {
                ViewData["Sch_Lin_Field"] = false;
            }

            if (FieldsValue[1].Contains("Ord_Qty_Field") == true)
            {
                ViewData["Ord_Qty_Field"] = true;
            }
            else
            {
                ViewData["Ord_Qty_Field"] = false;
            }

            if (FieldsValue[1].Contains("Open_Qty_Field") == true)
            {
                ViewData["Open_Qty_Field"] = true;
            }
            else
            {
                ViewData["Open_Qty_Field"] = false;
            }

            if (FieldsValue[1].Contains("Open_Amt_T_Field") == true)
            {
                ViewData["Open_Amt_T_Field"] = true;
            }
            else
            {
                ViewData["Open_Amt_T_Field"] = false;
            }

            if (FieldsValue[1].Contains("Order_Amt_Field") == true)
            {
                ViewData["Order_Amt_Field"] = true;
            }
            else
            {
                ViewData["Order_Amt_Field"] = false;
            }

            if (FieldsValue[1].Contains("Open_Amt_L_Field") == true)
            {
                ViewData["Open_Amt_L_Field"] = true;
            }
            else
            {
                ViewData["Open_Amt_L_Field"] = false;
            }

            if (FieldsValue[1].Contains("TSSC_Pending_Send_Date_Field") == true)
            {
                ViewData["TSSC_Pending_Send_Date_Field"] = true;
            }
            else
            {
                ViewData["TSSC_Pending_Send_Date_Field"] = false;
            }

            if (FieldsValue[1].Contains("Purchase_O_Field") == true)
            {
                ViewData["Purchase_O_Field"] = true;
            }
            else
            {
                ViewData["Purchase_O_Field"] = false;
            }
            writer.Write("po_number,");
            writer.Write("item,");
            if ((bool)ViewData["Status_Field"] == true)
                writer.Write("status,");

            if ((bool)ViewData["Description_Field"] == true)
                writer.Write("description,");

            if ((bool)ViewData["Delivery_Date_Field"] == true)
                writer.Write("delivery_date,");

            if ((bool)ViewData["Requisitioner_Field"] == true)
                writer.Write("requisitioner,");

            if ((bool)ViewData["DeliveryDate_Req_Field"] == true)
                writer.Write("delivery_date_request,");

            if ((bool)ViewData["Cost_Cent_Field"] == true)
                writer.Write("cost_center,");

            if ((bool)ViewData["Int_Order_Field"] == true)
                writer.Write("internal_order,");

            if ((bool)ViewData["Prof_Cent_Field"] == true)
                writer.Write("prof_center,");

            if ((bool)ViewData["Percentage_Field"] == true)
                writer.Write("percentage,");

            if ((bool)ViewData["cost_elem_Field"] == true)
                writer.Write("cost_element,");

            if ((bool)ViewData["co_el_name_Field"] == true)
                writer.Write("cost_element_name,");

            if ((bool)ViewData["Vendor_Field"] == true)
                writer.Write("vendor,");

            if ((bool)ViewData["VendorName_Field"] == true)
                writer.Write("vendor_name,");

            if ((bool)ViewData["UM_Field"] == true)
                writer.Write("um,");

            if ((bool)ViewData["T_Curr_Field"] == true)
                writer.Write("T_currency,");

            if ((bool)ViewData["Sch_Lin_Field"] == true)
                writer.Write("sch_lin,");

            if ((bool)ViewData["Ord_Qty_Field"] == true)
                writer.Write("order_quantity,");

            if ((bool)ViewData["Open_Qty_Field"] == true)
                writer.Write("open_quantity,");

            if ((bool)ViewData["Open_Amt_T_Field"] == true)
                writer.Write("open_amount_T,");

            if ((bool)ViewData["Order_Amt_Field"] == true)
                writer.Write("order_amount,");

            if ((bool)ViewData["Open_Amt_L_Field"] == true)
            {
                writer.Write("open_amount_L,");

            }
            if ((bool)ViewData["TSSC_Pending_Send_Date_Field"] == true)
                writer.Write("TSSC_Pending_Send_Date,");

            if ((bool)ViewData["Purchase_O_Field"] == true)
            {
                writer.Write("Purchase_O");

            }

            writer.WriteLine();
            foreach (PO po in pos)
            {
                if (((PONumValue[1] == "") || (po.PONumber.ToString() == PONumValue[1])) && ((ItemNumValue[1] == "") || (po.Item.ToString() == ItemNumValue[1])) && ((VendorValue[1] == "") || (po.Vendor.ToString() == VendorValue[1])) && ((StatusValue[1] == "") || (StatusValue[1] == "3") || (po.Status.ToString() == StatusValue[1])) && ((DeliveryDateFromValue[1] == "") || (po.DeliveryDate > DateTime.Parse(DeliveryDateFromValue[1]))) && ((DeliveryDateToValue[1] == "") || (po.DeliveryDate < DateTime.Parse(DeliveryDateToValue[1]))))
                {
                    writer.Write(po.PONumber); writer.Write(",");
                    writer.Write(po.Item); writer.Write(",");
                    if ((bool)ViewData["Status_Field"] == true)
                    {
                        writer.Write(po.Status); writer.Write(",");
                    }

                    if ((bool)ViewData["Description_Field"] == true)
                    {
                        String Descr_tmp = po.Descr.Replace(",", " ");
                        writer.Write(Descr_tmp); writer.Write(",");
                    }
                    if ((bool)ViewData["Delivery_Date_Field"] == true)
                    {
                        writer.Write(po.DeliveryDate); writer.Write(",");
                    }
                    if ((bool)ViewData["Requisitioner_Field"] == true)
                    {
                        writer.Write(po.Requisitioner); writer.Write(",");
                    }
                    if ((bool)ViewData["DeliveryDate_Req_Field"] == true)
                    {
                        writer.Write(po.DeliveryDate_Req); writer.Write(",");
                    }
                    if ((bool)ViewData["Cost_Cent_Field"] == true)
                    {
                        writer.Write(po.Cost_Cent); writer.Write(",");
                    }
                    if ((bool)ViewData["Int_Order_Field"] == true)
                    {
                        writer.Write(po.Int_Order); writer.Write(",");
                    }
                    if ((bool)ViewData["Prof_Cent_Field"] == true)
                    {
                        writer.Write(po.Prof_Cent); writer.Write(",");
                    }
                    if ((bool)ViewData["Percentage_Field"] == true)
                    {
                        writer.Write(po.Percentage); writer.Write(",");
                    }
                    if ((bool)ViewData["cost_elem_Field"] == true)
                    {
                        writer.Write(po.cost_elem); writer.Write(",");
                    }
                    if ((bool)ViewData["co_el_name_Field"] == true)
                    {
                        writer.Write(po.co_el_name); writer.Write(",");
                    }
                    if ((bool)ViewData["Vendor_Field"] == true)
                    {
                        writer.Write(po.Vendor); writer.Write(",");
                    }
                    if ((bool)ViewData["VendorName_Field"] == true)
                    {
                        String VendorName_tmp = po.Descr.Replace(",", " ");
                        writer.Write(VendorName_tmp); writer.Write(",");
                    }
                    if ((bool)ViewData["UM_Field"] == true)
                    {
                        writer.Write(po.UM); writer.Write(",");
                    }
                    if ((bool)ViewData["T_Curr_Field"] == true)
                    {
                        writer.Write(po.T_Curr); writer.Write(",");
                    }
                    if ((bool)ViewData["Sch_Lin_Field"] == true)
                    {
                        writer.Write(po.Sch_Lin); writer.Write(",");
                    }
                    if ((bool)ViewData["Ord_Qty_Field"] == true)
                    {
                        writer.Write(po.Ord_Qty); writer.Write(",");
                    }
                    if ((bool)ViewData["Open_Qty_Field"] == true)
                    {
                        writer.Write(po.Open_Qty); writer.Write(",");
                    }
                    if ((bool)ViewData["Open_Amt_T_Field"] == true)
                    {
                        writer.Write(po.Open_Amt_T); writer.Write(",");
                    }
                    if ((bool)ViewData["Order_Amt_Field"] == true)
                    {
                        writer.Write(po.Order_Amt); writer.Write(",");
                    }
                    if ((bool)ViewData["Open_Amt_L_Field"] == true)
                    {
                        writer.Write(po.Open_Amt_L); writer.Write(",");
                    }
                    if ((bool)ViewData["TSSC_Pending_Send_Date_Field"] == true)
                    {
                        writer.Write(po.TSSC_Pending_Send_Date); writer.Write(",");
                    }
                    if ((bool)ViewData["Purchase_O_Field"] == true)
                    {
                        writer.Write(po.Purchase_O);
                    }
                    writer.WriteLine();
                }
            }
            writer.Flush();
            output.Position = 0;

            return File(output, "text/comma-separated-values", "All_POs.csv");
        }

        public ActionResult Index(string searchPONum, string searchItemNum, string Fromdatepicker, string Todatepicker, string SearchStatus, string searchVendor, string Event,
                string Description_Field,
                string VendorName_Field,
                string Delivery_Date_Field,
                string Status_Field,
                string Requisitioner_Field,
                string DeliveryDate_Req_Field,
                 string   Cost_Cent_Field,
                 string   Int_Order_Field,
                 string   Prof_Cent_Field,
                 string   Percentage_Field,
                 string   cost_elem_Field,
                string    co_el_name_Field,
                string    Vendor_Field,
                string    UM_Field,
                string    T_Curr_Field,
                string    Sch_Lin_Field,
               string     Ord_Qty_Field,
               string     Open_Qty_Field,
                string    Open_Amt_T_Field,
                string    Order_Amt_Field,
               string     Open_Amt_L_Field,
                            string TSSC_Pending_Send_Date_Field,
                string Purchase_O_Field)
        {

            Session["Came_From"] = "AllPOsList";
            
            var flag = 0;
            if (!string.IsNullOrEmpty(Event))
            {
                if (Event == "Export This Data") 
                {
                    flag = 1;
                }
            }

            var user_id = User.Identity.Name;
            user_id = user_id.Replace("\\", ",");
            var words = user_id.Split(new Char[] { ',' });
            user_id = words[1];

            var pos_list = from poes in db3.POs 
                      where (poes.latest == 1)
                      select poes;
            
            var mail_add = from users in db3.Papi_Users_tbl
                           where (users.domain_username == user_id)
                           select users.email;
            var users_list = from users in db3.Papi_Users_tbl
                             where (users.domain_username == user_id)
                             select users;

            var is_superuser = false;

            if (users_list.Count() > 0)
            {
                is_superuser = users_list.First().is_superuser;
            }

            var cost_cent = from users in db3.Papi_Users_tbl
                            where (users.domain_username == user_id)
                            select users.Cost_Cent;
            string[] cost_centArray = cost_cent.ToArray();
            String scost_cent = cost_centArray[0];

            var int_Order = from users in db3.Papi_Users_tbl
                            where (users.domain_username == user_id)
                            select users.Int_Order;

            string[] int_OrderArray = int_Order.ToArray();
            String sint_Order = int_OrderArray[0];

            var requisitioner = from users in db3.Papi_Users_tbl
                            where (users.domain_username == user_id)
                            select users.Requisitio;

            string[] requisitionerArray = requisitioner.ToArray();
            String sRequisitioner = requisitionerArray[0];

            IQueryable<PO> pos_req = null;
            IQueryable<PO> pos_cc = null;
            IQueryable<PO> pos_io = null;
            IQueryable<PO> pos = null;

            if (!is_superuser)
            {
                pos_req = (from users in db3.Papi_Users_tbl
                           where (users.domain_username == user_id)
                           join poes in pos_list
                           on users.Requisitio equals poes.Requisitioner
                           select poes);

                pos_cc = (from users in db3.Papi_Users_tbl
                          where (users.domain_username == user_id)
                          join poes in pos_list
                          on users.Cost_Cent equals poes.Cost_Cent
                          select poes);
                pos_io = (from users in db3.Papi_Users_tbl
                          where (users.domain_username == user_id)
                          join poes in pos_list
                          on users.Int_Order equals poes.Int_Order
                          select poes);
                pos = (from poes in pos_req select poes)
                      .Union
                      (from poes1 in pos_cc select poes1)
                      .Union
                      (from poes2 in pos_io select poes2);
            }

            else 
            {
                pos = pos_list;
            }


            string FilterString = users_list.First().filter;

            if (string.IsNullOrEmpty(FilterString)) {
                FilterString = "PONum=&ItemNum=&DeliveryDateFrom=&DeliveryDateTo=&Status=3&Vendor=&Fields=";
            }

            if (!string.IsNullOrEmpty(Event))
            {
                if (Event == "Filter")
                {
                   FilterString = string.Concat("PONum=", searchPONum, "&", "ItemNum=", searchItemNum, "&", "DeliveryDateFrom=", Fromdatepicker, "&", "DeliveryDateTo=", Todatepicker, "&", "Status=", SearchStatus, "&", "Vendor=", searchVendor, "&", "Fields=");

                   if (!string.IsNullOrEmpty(Description_Field))
                   {
                       FilterString = string.Concat(FilterString, "Description_Field");
                   }

                   if (!string.IsNullOrEmpty(VendorName_Field))
                   {
                       FilterString = string.Concat(FilterString, "VendorName_Field");
                   }
                   if (!string.IsNullOrEmpty(Delivery_Date_Field))
                   {
                       FilterString = string.Concat(FilterString, "Delivery_Date_Field");
                   }
                   if (!string.IsNullOrEmpty(Status_Field))
                   {
                       FilterString = string.Concat(FilterString, "Status_Field");
                   }
                   if (!string.IsNullOrEmpty(Requisitioner_Field))
                   {
                       FilterString = string.Concat(FilterString, "Requisitioner_Field");
                   }
                   if (!string.IsNullOrEmpty(DeliveryDate_Req_Field))
                   {
                       FilterString = string.Concat(FilterString, "DeliveryDate_Req_Field");
                   }
                   if (!string.IsNullOrEmpty(Cost_Cent_Field))
                   {
                       FilterString = string.Concat(FilterString, "Cost_Cent_Field");
                   }
                   if (!string.IsNullOrEmpty(Int_Order_Field))
                   {
                       FilterString = string.Concat(FilterString, "Int_Order_Field");
                   }
                   if (!string.IsNullOrEmpty(Prof_Cent_Field))
                   {
                       FilterString = string.Concat(FilterString, "Prof_Cent_Field");
                   }
                   if (!string.IsNullOrEmpty(Percentage_Field))
                   {
                       FilterString = string.Concat(FilterString, "Percentage_Field");
                   }
                   if (!string.IsNullOrEmpty(cost_elem_Field))
                   {
                       FilterString = string.Concat(FilterString, "cost_elem_Field");
                   }
                   if (!string.IsNullOrEmpty(co_el_name_Field))
                   {
                       FilterString = string.Concat(FilterString, "co_el_name_Field");
                   }
                   if (!string.IsNullOrEmpty(Vendor_Field))
                   {
                       FilterString = string.Concat(FilterString, "Vendor_Field");
                   }
                   if (!string.IsNullOrEmpty(UM_Field))
                   {
                       FilterString = string.Concat(FilterString, "UM_Field");
                   }
                   if (!string.IsNullOrEmpty(T_Curr_Field))
                   {
                       FilterString = string.Concat(FilterString, "T_Curr_Field");
                   }
                   if (!string.IsNullOrEmpty(Sch_Lin_Field))
                   {
                       FilterString = string.Concat(FilterString, "Sch_Lin_Field");
                   }
                   if (!string.IsNullOrEmpty(Ord_Qty_Field))
                   {
                       FilterString = string.Concat(FilterString, "Ord_Qty_Field");
                   }
                   if (!string.IsNullOrEmpty(Open_Qty_Field))
                   {
                       FilterString = string.Concat(FilterString, "Open_Qty_Field");
                   }
                   if (!string.IsNullOrEmpty(Open_Amt_T_Field))
                   {
                       FilterString = string.Concat(FilterString, "Open_Amt_T_Field");
                   }
                   if (!string.IsNullOrEmpty(Order_Amt_Field))
                   {
                       FilterString = string.Concat(FilterString, "Order_Amt_Field");
                   }
                   if (!string.IsNullOrEmpty(Open_Amt_L_Field))
                   {
                       FilterString = string.Concat(FilterString, "Open_Amt_L_Field");
                   }

                   if (!string.IsNullOrEmpty(TSSC_Pending_Send_Date_Field))
                   {
                       FilterString = string.Concat(FilterString, "TSSC_Pending_Send_Date_Field");
                   }
                   if (!string.IsNullOrEmpty(Purchase_O_Field))
                   {
                       FilterString = string.Concat(FilterString, "Purchase_O_Field");
                   }

                   db3.Papi_Users_tbl.Where(p => p.domain_username == users_list.FirstOrDefault().domain_username).FirstOrDefault().filter = FilterString;
                   db3.SaveChanges();
                    
                }
            }

            string[] splitArray = FilterString.Split('&');

            string[] PONumValue = splitArray[0].Split('=');
            ViewData["PONumValue"] = PONumValue[1];

            string[] ItemNumValue = splitArray[1].Split('=');
            ViewData["ItemNumValue"] = ItemNumValue[1];

            string[] DeliveryDateFromValue = splitArray[2].Split('=');
            ViewData["DeliveryDateFromValue"] = DeliveryDateFromValue[1];

            string[] DeliveryDateToValue = splitArray[3].Split('=');
            ViewData["DeliveryDateToValue"] = DeliveryDateToValue[1];

            string[] StatusValue = splitArray[4].Split('=');
            ViewData["StatusValue"] = StatusValue[1];

            string[] VendorValue = splitArray[5].Split('=');
            ViewData["VendorValue"] = VendorValue[1];

            string[] FieldsValue = splitArray[6].Split('=');

            if (FieldsValue[1].Contains("Description_Field") == true)
            {
                ViewData["Description_Field"] = true;
            }
            else
            {
                ViewData["Description_Field"] = false;
            }

            if (FieldsValue[1].Contains("VendorName_Field") == true)
            {
                ViewData["VendorName_Field"] = true;
            }
            else
            {
                ViewData["VendorName_Field"] = false;
            }

            if (FieldsValue[1].Contains("Delivery_Date_Field") == true)
            {
                ViewData["Delivery_Date_Field"] = true;
            }
            else
            {
                ViewData["Delivery_Date_Field"] = false;
            }

            if (FieldsValue[1].Contains("Status_Field") == true)
            {
                ViewData["Status_Field"] = true;
            }
            else
            {
                ViewData["Status_Field"] = false;
            }

            if (FieldsValue[1].Contains("Requisitioner_Field") == true)
            {
                ViewData["Requisitioner_Field"] = true;
            }
            else
            {
                ViewData["Requisitioner_Field"] = false;
            }

            if (FieldsValue[1].Contains("DeliveryDate_Req_Field") == true)
            {
                ViewData["DeliveryDate_Req_Field"] = true;
            }
            else
            {
                ViewData["DeliveryDate_Req_Field"] = false;
            }

            if (FieldsValue[1].Contains("Cost_Cent_Field") == true)
            {
                ViewData["Cost_Cent_Field"] = true;
            }
            else
            {
                ViewData["Cost_Cent_Field"] = false;
            }

            if (FieldsValue[1].Contains("Int_Order_Field") == true)
            {
                ViewData["Int_Order_Field"] = true;
            }
            else
            {
                ViewData["Int_Order_Field"] = false;
            }

            if (FieldsValue[1].Contains("Prof_Cent_Field") == true)
            {
                ViewData["Prof_Cent_Field"] = true;
            }
            else
            {
                ViewData["Prof_Cent_Field"] = false;
            }

            if (FieldsValue[1].Contains("Percentage_Field") == true)
            {
                ViewData["Percentage_Field"] = true;
            }
            else
            {
                ViewData["Percentage_Field"] = false;
            }

            if (FieldsValue[1].Contains("cost_elem_Field") == true)
            {
                ViewData["cost_elem_Field"] = true;
            }
            else
            {
                ViewData["cost_elem_Field"] = false;
            }


            if (FieldsValue[1].Contains("co_el_name_Field") == true)
            {
                ViewData["co_el_name_Field"] = true;
            }
            else
            {
                ViewData["co_el_name_Field"] = false;
            }

            if (FieldsValue[1].Contains("Vendor_Field") == true)
            {
                ViewData["Vendor_Field"] = true;
            }
            else
            {
                ViewData["Vendor_Field"] = false;
            }

            if (FieldsValue[1].Contains("UM_Field") == true)
            {
                ViewData["UM_Field"] = true;
            }
            else
            {
                ViewData["UM_Field"] = false;
            }

            if (FieldsValue[1].Contains("T_Curr_Field") == true)
            {
                ViewData["T_Curr_Field"] = true;
            }
            else
            {
                ViewData["T_Curr_Field"] = false;
            }

            if (FieldsValue[1].Contains("Sch_Lin_Field") == true)
            {
                ViewData["Sch_Lin_Field"] = true;
            }
            else
            {
                ViewData["Sch_Lin_Field"] = false;
            }

            if (FieldsValue[1].Contains("Ord_Qty_Field") == true)
            {
                ViewData["Ord_Qty_Field"] = true;
            }
            else
            {
                ViewData["Ord_Qty_Field"] = false;
            }

            if (FieldsValue[1].Contains("Open_Qty_Field") == true)
            {
                ViewData["Open_Qty_Field"] = true;
            }
            else
            {
                ViewData["Open_Qty_Field"] = false;
            }

            if (FieldsValue[1].Contains("Open_Amt_T_Field") == true)
            {
                ViewData["Open_Amt_T_Field"] = true;
            }
            else
            {
                ViewData["Open_Amt_T_Field"] = false;
            }

            if (FieldsValue[1].Contains("Order_Amt_Field") == true)
            {
                ViewData["Order_Amt_Field"] = true;
            }
            else
            {
                ViewData["Order_Amt_Field"] = false;
            }

            if (FieldsValue[1].Contains("Open_Amt_L_Field") == true)
            {
                ViewData["Open_Amt_L_Field"] = true;
            }
            else
            {
                ViewData["Open_Amt_L_Field"] = false;
            }
            if (FieldsValue[1].Contains("TSSC_Pending_Send_Date_Field") == true)
            {
                ViewData["TSSC_Pending_Send_Date_Field"] = true;
            }
            else
            {
                ViewData["TSSC_Pending_Send_Date_Field"] = false;
            }

            if (FieldsValue[1].Contains("Purchase_O_Field") == true)
            {
                ViewData["Purchase_O_Field"] = true;
            }
            else
            {
                ViewData["Purchase_O_Field"] = false;
            }
     

            if (!string.IsNullOrEmpty(PONumValue[1]))
                {
                    try
                    {
                        var x = Convert.ToInt64(PONumValue[1]);
                        pos = pos.Where(s => s.PONumber == x);
                    }
                    catch
                    {
                        ModelState.AddModelError("searchPONum", "PO Number Input Error");
                    }
                }

            if (!string.IsNullOrEmpty(ItemNumValue[1]))
                {
                    try
                    {
                        var y = Convert.ToInt32(ItemNumValue[1]);
                        pos = pos.Where(s => s.Item == y);
                    }
                    catch
                    {
                        ModelState.AddModelError("searchItemNum", "Item Number Input Error");
                    }
                }

            if (!string.IsNullOrEmpty(DeliveryDateFromValue[1]))
                {
                    try
                    {
                        DateTime z = Convert.ToDateTime(DeliveryDateFromValue[1]);
                        pos = pos.Where(s => (DateTime.Compare(s.DeliveryDate, z) >= 0));
                    }
                    catch
                    {
                        ModelState.AddModelError("Fromdatepicker", "From Date Input Error");
                    }
                }

            if (!string.IsNullOrEmpty(DeliveryDateToValue[1]))
                {
                    try
                    {
                        DateTime q = Convert.ToDateTime(DeliveryDateToValue[1]);
                        pos = pos.Where(s => (DateTime.Compare(s.DeliveryDate, q) <= 0));
                    }
                    catch
                    {
                        ModelState.AddModelError("Todatepicker", "To Date Input Error");
                    }
                }

            if ((string.Compare(StatusValue[1], "3") != 0) && (!string.IsNullOrEmpty(StatusValue[1])))
                {
                    var r = Convert.ToInt32(StatusValue[1]);
                    pos = pos.Where(s => s.Status == r);
                }

                if (!string.IsNullOrEmpty(VendorValue[1]))
                {
                    pos = pos.Where(s => s.VendorName.Contains(VendorValue[1]));
                    //pos = pos.Where(s => (string.Compare(searchVendor, s.VendorName) == 0));
                }
                if (flag != 1)
                {
                    return View(pos);
                }
                else
                {
                    
                    //Application app = new Application();
                    //app.Visible = false;

                    //Workbook workbook = app.Workbooks.Add(1);
                    //Worksheet worksheet = (Worksheet)workbook.Sheets[1];
                    ////Add Table
                    //int i = 1;

                    //PO[] po_arr = pos.ToArray();

                    //    worksheet.Cells[1, i] = "PO_Number";
                    //    i++;
       
                    //    worksheet.Cells[1, i] = "Item";
                    //    i++;
        
                    //    if (Description_Field=="1")
                    //    {
                    //        try
                    //        {
                    //            worksheet.Cells[1, i] = "Description";
                    //            i++;
                    //        }
                    //        catch
                    //        {
                    //            ModelState.AddModelError("Description_Field", "Description Field Input Error");
                    //        }
                    //    }

                    //    if (Delivery_Date_Field == "1")
                    //    {
                    //        try
                    //        { 
                    //            worksheet.Cells[1, i] = "Delivery Date";
                    //            i++;
                    //        }
                    //        catch
                    //        {
                    //            ModelState.AddModelError("Delivery_Date_Field", "Delivery_Date_Field Input Error");
                    //        }
                    //    }

                    //    if (Status_Field == "1")
                    //    {
                    //        try
                    //        {
                    //            worksheet.Cells[1, i] = "Status";
                    //            i++;
                    //        }
                    //        catch
                    //        {
                    //            ModelState.AddModelError("Status_Field", "Status Field Input Error");
                    //        }
                    //    }

                    //    if (Requisitioner_Field == "1")
                    //    {
                    //        try
                    //        {
                    //            worksheet.Cells[1, i] = "Requisitioner";
                    //            i++;
                    //        }
                    //        catch
                    //        {
                    //            ModelState.AddModelError("Requisitioner_Field", "Requisitioner Field Input Error");
                    //        }
                    //    }

                    //    if (DeliveryDate_Req_Field == "1")
                    //    {
                    //        try
                    //        {
                    //            worksheet.Cells[1, i] = "Delivery Date_Req";
                    //            i++;
                    //        }
                    //        catch
                    //        {
                    //            ModelState.AddModelError("DeliveryDate_Req_Field", "DeliveryDate_Req Field Input Error");
                    //        }
                    //    }

                    //    if (Cost_Cent_Field == "1")
                    //    {
                    //        try
                    //        {
                    //            worksheet.Cells[1, i] = "Cost Cent Field";
                    //            i++;
                    //        }
                    //        catch
                    //        {
                    //            ModelState.AddModelError("Cost_Cent_Field", "Cost Cent Field Input Error");
                    //        }
                    //    }

                    //    if (Int_Order_Field == "1")
                    //    {
                    //        try
                    //        {
                    //            worksheet.Cells[1, i] = "Int Order Field";
                    //            i++;
                    //        }
                    //        catch
                    //        {
                    //            ModelState.AddModelError("Int_Order_Field", "Int_Order_Field Input Error");
                    //        }
                    //    }
                    //    if (Prof_Cent_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "Prof Cent Field";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("Prof_Cent_Field", "Prof_Cent_Field Input Error");
                    //    }
                    //}

                    //    if (Percentage_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "Percentage";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("Percentage_Field", "Percentage_Field Input Error");
                    //    }
                    //}
                    //    if (cost_elem_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "cost elem Field";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("cost_elem_Field", "cost_elem_Field Input Error");
                    //    }
                    //}

                    //    if (co_el_name_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "co el name";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("co_el_name_Field", "co_el_name_Field Input Error");
                    //    }
                    //}

                    //    if (Vendor_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "Vendor";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("Vendor_Field", "Vendor_Field Input Error");
                    //    }
                    //}

                    //    if (VendorName_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "Vendor Name";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("VendorName_Field", "VendorName_Field Input Error");
                    //    }
                    //}

                    //    if (UM_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "UM";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("UM_Field", "UM_Field Input Error");
                    //    }
                    //}

                    //    if (T_Curr_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "T_Curr";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("T_Curr_Field", "T_Curr_Field Input Error");
                    //    }
                    //}

                    //    if (Sch_Lin_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "Sch Lin";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("Sch_Lin_Field", "Sch_Lin_Field Input Error");
                    //    }
                    //}

                    //    if (Ord_Qty_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "Ord Qty";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("Ord_Qty_Field", "Ord_Qty_Field Input Error");
                    //    }
                    //}

                    //    if (Open_Qty_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "Open Qty";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("Open_Qty_Field", "Open_Qty_Field Input Error");
                    //    }
                    //}

                    //    if (Open_Amt_T_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "Open_Amt_T";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("Open_Amt_T_Field", "Open_Amt_T_Field Input Error");
                    //    }
                    //}
                    //    if (Order_Amt_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "Order_Amt";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("Order_Amt_Field", "Order_Amt_Field Input Error");
                    //    }
                    //}
                    //    if (Open_Amt_L_Field == "1")
                    //{
                    //    try
                    //    {
                    //        worksheet.Cells[1, i] = "Open_Amt_L";
                    //        i++;
                    //    }
                    //    catch
                    //    {
                    //        ModelState.AddModelError("Open_Amt_L_Field", "Open_Amt_L_Field Input Error");
                    //    }
                    //}



                    //i = 1;
                    //int j = 2;
                    //int count1 = pos.Count();
                    //while (j < 2 + count1)
                    //{
                       
                    //        worksheet.Cells[j, i] = po_arr[j - 2].PONumber;
                    //        i++;
                        
                    
                    //        worksheet.Cells[j, i] = po_arr[j - 2].Item;
                    //        i++;

                    //        if (Description_Field=="1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Descr.ToString();
                    //                i++;
                            
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Description_Field", "Description Field Input Error");
                    //            }
                    //        }

                    //        if (Delivery_Date_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].DeliveryDate.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Delivery_Date_Field", "Delivery_Date_Field Input Error");
                    //            }
                    //        }

                    //        if (Status_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                if (po_arr[j - 2].Status == 0)
                    //                    worksheet.Cells[j, i] = "OPEN";
                    //                else if (po_arr[j - 2].Status == 1)
                    //                    worksheet.Cells[j, i] = "PENDING";
                    //                else
                    //                    worksheet.Cells[j, i] = "CLOSED";
                    //                i++;

                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Status_Field", "Status Field Input Error");
                    //            }
                    //        }
                    //        if (Requisitioner_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Requisitioner.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Requisitioner_Field", "Requisitioner Field Input Error");
                    //            }
                    //        }

                    //        if (DeliveryDate_Req_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].DeliveryDate_Req.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("DeliveryDate_Req_Field", "DeliveryDate_Req Field Input Error");
                    //            }
                    //        }

                    //        if (Cost_Cent_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Cost_Cent.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Cost_Cent_Field", "Cost Cent Field Input Error");
                    //            }
                    //        }
                    //        if (Int_Order_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Int_Order.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Int_Order_Field", "Int_Order_Field Input Error");
                    //            }
                    //        }
                    //        if (Prof_Cent_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Prof_Cent.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Prof_Cent_Field", "Prof_Cent_Field Input Error");
                    //            }
                    //        }

                    //        if (Percentage_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Percentage.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Percentage_Field", "Percentage_Field Input Error");
                    //            }
                    //        }
                    //        if (cost_elem_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].cost_elem.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("cost_elem_Field", "cost_elem_Field Input Error");
                    //            }
                    //        }

                    //        if (co_el_name_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].co_el_name.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("co_el_name_Field", "co_el_name_Field Input Error");
                    //            }
                    //        }

                    //        if (Vendor_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Vendor.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Vendor_Field", "Vendor_Field Input Error");
                    //            }
                    //        }

                    //        if (VendorName_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].VendorName.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("VendorName_Field", "VendorName_Field Input Error");
                    //            }
                    //        }

                    //        if (UM_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].UM.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("UM_Field", "UM_Field Input Error");
                    //            }
                    //        }

                    //        if (T_Curr_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].T_Curr.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("T_Curr_Field", "T_Curr_Field Input Error");
                    //            }
                    //        }

                    //        if (Sch_Lin_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Sch_Lin.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Sch_Lin_Field", "Sch_Lin_Field Input Error");
                    //            }
                    //        }

                    //        if (Ord_Qty_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Ord_Qty.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Ord_Qty_Field", "Ord_Qty_Field Input Error");
                    //            }
                    //        }

                    //        if (Open_Qty_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Open_Qty.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Open_Qty_Field", "Open_Qty_Field Input Error");
                    //            }
                    //        }

                            
                    //        /*
                    //        if (Order_Amt_T_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Order_Amt_T.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Order_Amt_T_Field", "Order_Amt_T_Field Input Error");
                    //            }
                    //        }
                    //         */

                    //        if (Open_Amt_T_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Open_Amt_L.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Open_Amt_T_Field", "Open_Amt_T_Field Input Error");
                    //            }
                    //        }
                    //        if (Order_Amt_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Order_Amt.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Order_Amt_Field", "Order_Amt_Field Input Error");
                    //            }
                    //        }
                    //        if (Open_Amt_L_Field == "1")
                    //        {
                    //            try
                    //            {
                    //                worksheet.Cells[j, i] = po_arr[j - 2].Open_Amt_L.ToString();
                    //                i++;
                    //            }
                    //            catch
                    //            {
                    //                ModelState.AddModelError("Open_Amt_L_Field", "Open_Amt_L_Field Input Error");
                    //            }
                    //        }
                        

                    //    i = 1;
                    //    j++;

                    //}
                    //app.DisplayAlerts = false;
                    //try
                    //{
                    //    app.SaveWorkspace();
                        
                    //}

          
                    //catch {
                    //    app.Quit();
                    //    return View(pos);
                    //}
                    //app.Quit();
                    return View(pos);
                }

                }

    }
}
